package com.example.pages;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import com.beust.jcommander.internal.Lists;
import com.sun.jna.platform.win32.Netapi32Util.User;

public class UsersPage extends InternalPage {
	@FindBy(name = "username")
	public WebElement usernameField;

	@FindBy(name = "email")
	public WebElement emailField;

	@FindBy(name = "password")
	public WebElement passwordField;

	@FindBy(name = "password2")
	public WebElement passwordConfirmationField;

	@FindBy(name = "submit")
	public WebElement saveButton;

	@FindBy(css = "div#user-content table > tbody > tr")
	public java.util.List<WebElement> userRows;
	
	public UsersPage createUser(User user) {
		usernameField.sendKeys(user.name);
		emailField.sendKeys(user.email);
		passwordField.clear();
		passwordField.sendKeys(user.password);
		passwordConfirmationField.sendKeys(user.password);
		saveButton.click();
		return MyPageFactory.getPage(driver, UsersPage.class);
	}

	public List<com.example.tests.User> getUsers() {
		java.util.List<User> users = Lists.newArrayList();
		for (WebElement row : userRows) {
		users.add(convertRowToUser(row));	
		}
		return users;
	}

	private User convertRowToUser(WebElement row) {
		java.util.List<WebElement> cells - row.findElements(By.tagName("td"));
		return new User()
		.withName(cells.get(1).getText())
		.withEmail(cells.get(2).getText())
		.withRole(cells.get(3).getText());
	}

}
